package cn.stylefeng.roses.kernel.conversion.util;

import cn.hutool.core.io.resource.ResourceUtil;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * pdf相关工具类
 *
 * @author fengshuonan
 * @date 2021/11/1 10:55
 */
public class PdfUtil {

    public static PdfTrueTypeFont trueTypeFont = null;

    static {
        trueTypeFont = new PdfTrueTypeFont(ResourceUtil.getStream("fonts/msyh.ttf"), 20f);
    }

    /**
     * pdf文件添加图片水印
     *
     * @param originFileStream    原始pdf文件流
     * @param watermarkFileStream 图片水印文件流
     * @param outputStream        输出的文件流
     * @author fengshuonan
     * @date 2021/11/1 10:56
     */
    public static void addPicWatermark(InputStream originFileStream, InputStream watermarkFileStream, OutputStream outputStream) {

        //创建PdfDocument对象,并加载PDF测试文档
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromStream(originFileStream);

        PdfImage image = PdfImage.fromStream(watermarkFileStream);

        //遍历文档每一页,加载图片，并设置成平铺背景（水印）
        for (int i = 0; i < pdf.getPages().getCount(); i++) {
            PdfPageBase page = pdf.getPages().get(i);

            Dimension2D dimension2D = new Dimension();
            dimension2D.setSize(page.getCanvas().getSize().getWidth() / 2, page.getCanvas().getSize().getHeight() / 3);

            PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
            brush.getGraphics().setTransparency(0.4f);
            brush.getGraphics().translateTransform(brush.getSize().getWidth() / 2, brush.getSize().getHeight() / 2);
            brush.getGraphics().rotateTransform(-45);

            brush.getGraphics().drawImage(image, 0, 0);

            Rectangle2D rectangle2D = new Rectangle2D.Float();
            rectangle2D.setFrame(new Point(0, 0), page.getCanvas().getClientSize());

            page.getCanvas().drawRectangle(brush, rectangle2D);
        }

        //保存文档
        pdf.saveToStream(outputStream);
        pdf.dispose();
    }

    /**
     * pdf文件添加文字水印
     *
     * @param originPdfFileStream 被添加水印的pdf流
     * @param watermark           水印文字
     * @param outputPdfStream     生成pdf文件的路径
     * @author fengshuonan
     * @date 2021/11/1 15:27
     */
    public static void addTextWatermark(InputStream originPdfFileStream, String watermark, OutputStream outputPdfStream) {

        // 创建PdfDocument对象，加载
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromStream(originPdfFileStream);

        // 每一页都加水印
        for (int i = 0; i < pdf.getPages().getCount(); i++) {
            PdfPageBase page = pdf.getPages().get(i);
            Dimension2D dimension2D = new Dimension();
            dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
            PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
            brush.getGraphics().setTransparency(0.3F);
            brush.getGraphics().save();
            brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
            brush.getGraphics().rotateTransform(-45);
            brush.getGraphics().drawString(watermark, trueTypeFont, PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
            brush.getGraphics().restore();
            brush.getGraphics().setTransparency(1);
            Rectangle2D loRect = new Rectangle2D.Float();
            loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
            page.getCanvas().drawRectangle(brush, loRect);
        }

        //保存文档
        pdf.saveToStream(outputPdfStream);
    }

}
